<?php
//error_reporting(E_ALL);
//ini_set('display_errors', 1);

$this->useClass("Handler_Community");
class Action_Community_RssList extends Handler_Community {
  private $lib_rss;
  private $pages_wing = 3;
  
  public function process() {
		$aa = $this->aa;
		$acl_data = $aa->getInfo();
		if (!$aa->is_granted('community_edit')) return;
    
		// -- old theme HACK : 2008-05-21 : lucky -- {{{
		$community_skin = 'platonica';
		$tpl = $this->ctx->tpl;
		$community_model = $this->ctx->mm->getModel("Community");
		$community_model->setDefaultSkin($community_skin);
		$tpl->setSkin($community_skin);
		$views_php = $tpl->findFile('', 'views', $tpl->_skin, 'php');
		unset ($config);
		include $views_php; // got $config
		$this->ctx->rh->views->setViewConfig($config);
		// -- old theme HACK : 2008-05-21 : lucky -- }}}
    
    $view = $this->rh->views->findByName('list_rss');
    $view->data = $aa->template_data;
    
    $model = $this->ctx->mm->getModel("Themes", "m");
    $model->setDisablePages();
    $themes = $model->getThemes();
    if ( !empty($themes) ) {
      $view->data['themes'] = $themes;
    }
    
    if ( isset($this->rh->POST) && !empty($this->rh->POST) ) {
      $oid = (integer) abs($this->rh->POST['oid']);
      $title = htmlspecialchars(stripcslashes(trim($this->rh->POST['title'])));
      $url = stripcslashes(trim($this->rh->POST['url']));
      $parent_oid = (integer) abs($this->rh->POST['theme']);
      
      $rfmodel = $this->ctx->mm->getModel("RssFeedList", "m");
      $rfmodel->setCommunityOid($acl_data['comity_oid']);
      
      if ( !empty($url) ) {
        $this->rh->UseClass("RL_URLGenerator");
        $this->rh->UseClass("RSSAggregator_RssLibWrapper");
    		$this->lib_rss = new RssLibWrapper();
        $rss = 	$this->lib_rss->fetch($url);
        
        if ( $rss instanceof MagpieRSS ) {
          if ( $oid > 0 ) {
            $result = $rfmodel->updateFeed($oid, $url, $rss, $parent_oid, $title);
            if ( 0 == $result ) {
              $this->rh->setMessage('Лента успешно обновлена.');
            }
          } else {
            $result = $rfmodel->addFeed($url, $rss, $parent_oid, $title);
            if ( 0 == $result ) {
              $this->rh->setMessage('Лента успешно добавлена.');
            }
          }
          if ( 1 == $result ) {
            $this->rh->setMessage('Ошибка при записи.');
          } else if ( 2 == $result ) {
            $this->rh->setMessage('Подписка по этому адресу уже существует.');
          } else if ( 0 == $result ) {
            $rh = $this->rh;
            $store = $this->rh->ctx->store;
            $db = $this->rh->ctx->store->db;
            
            require_once('lib/rss_publish.php');
          }
        } else {
          $this->rh->setMessage('Лента по адресу ' . htmlspecialchars($url) . '  не найдена.');
        }
      }

      if ( isset($this->rh->REQUEST['from_theme']) && !empty($this->rh->REQUEST['from_theme']) ) {
        $location = "{$this->aa->acl_data['base_url']}/{$acl_data['community_url']}/theme/{$parent_oid}";
      } else {
        $location = "{$this->aa->acl_data['base_url']}/{$acl_data['community_url']}/rss_list";
      }
      if ( isset($this->rh->REQUEST['show_all']) && !empty($this->rh->REQUEST['show_all']) ) {
        $location .= '?show_all=1';
      } else {
        $lpage = (integer) abs($this->rh->REQUEST['lpage']+0);
        if ( $lpage > 0 ) {
          $location .= "?lpage={$lpage}";
        }
      }
        header('Location: ' . $location);
        exit;
    }
    
    $rssmodel = $this->ctx->mm->getModel("RssFeed", "m");
    if ( isset($this->rh->GET['lpage']) && !empty($this->rh->GET['lpage']) ) {
      $rssmodel->setLPage((integer) abs($this->rh->GET['lpage']+0));
    }
    if ( isset($this->rh->GET['show_all']) && !empty($this->rh->GET['show_all']) ) {
      $rssmodel->setDisablePages();
      $view->data['show_all'] = true;
    }
    $view->data['rssfeed'] = $rssmodel->getRssFeedList();

    $pages = $rssmodel->getPages();
    if ( $pages > 1 ) {
      $lpage = $rssmodel->getLPage();
      if ( $lpage > 1 ) {
        $min = $lpage - $this->pages_wing;
        if ( $min < 1 ) {
          $min = 1;
        }
      } else {
        $min = 1;
      }
      
      $max = $lpage + $this->pages_wing;
      if ( $max > $pages ) {
        $max = $pages;
      }

      $pager = array();
      for ( $i=$min; $i<=$max; $i++ ) {
        $pager[] = array( 'lpage'=>$i );
        if ( $i == $lpage ) {
          $pager[count($pager)-1]['current'] = true;
        }
      }
      
      if ( $max < $pages ) {
        $view->data['max_page'] = $pages;
      }
      if ( $min > 1 ) {
        $view->data['min_page'] = 1;
      }
      
      $view->data['pager'] = $pager;
      $view->data['lpage'] = $lpage;
    }

    $view->data['rss_tab'] = true;
    
    $this->rh->response->setContent($view);
  }
}